/**
 * 项目名称:
 *
 * @Author: 夏国倩
 * @Date: 2023/10/13 15:39
 * @Copyright
 */
package wait;
public class Input implements Runnable{
    private Resource r;

    public Input(Resource r) {
        this.r = r;
    }

    @Override
    public void run() {
        int i=0;
        while (true){
            synchronized (r) {
                if (r.flag){ //标记是true等待
                    try{
                        wait();
                    }catch (Exception e){
                    }
                }
                if (i%2==0){
                    r.name = "张三";
                    r.sex = "男";
                } else {
                    r.name = "李四";
                    r.sex = "女";
                }
                //将对方线程唤醒，标记为true
                r.flag=true;
                r.notify();
            }
            i++;
        }
    }
}
